package canss.webservice.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import canss.supports.basic;
import canss.webservice.model.ContentDesc;
import canss.webservice.model.Feed;
import canss.webservice.service.PortalIntraWebServiceControl;
import canss.webservice.service.PortalIntraWebServiceControlBean;

public class svtCPHome extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet
{
	private static final long serialVersionUID = -6913413413412200434L;

	public svtCPHome()
	{
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		doPost(request, response);
	}

	PortalIntraWebServiceControl control;
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{

		response.setContentType("text/html; charset=UTF-8");

		PrintWriter out = response.getWriter();

		String _pid = request.getParameter("_pid");
		String _notxml = request.getParameter("_notxml");

		control = (control == null) ? new PortalIntraWebServiceControlBean() : control;

		if (_notxml != null)
		{// in html
		} else
		{// in xml
			out.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
			weblogic.application.utils.XMLWriter writer = new weblogic.application.utils.XMLWriter(out);

			writer.addElement("canss");
			writer.addElement("inform");
			{
				/*
				 * contents
				 */
				if(!basic.isEmpty(_pid))
				{
					ContentDesc[] entirecid = control.getContentDescByPID(_pid);
					if(entirecid!=null)
					{
						writer.addElement("numof_contents", ""+entirecid.length);
					
						int numof_pub = 0;
						int numof_uploading = 0;
						int numof_encoding = 0;
						int numof_encoded = 0;
						for(ContentDesc content : entirecid)
						{
							if(content.getCANSSCONTENTDESCRIPTION_STATUS() == basic.PUBLISH_STATUS)
								numof_pub++;
							else if(content.getCANSSCONTENTDESCRIPTION_STATUS() >= basic.LOC_AGGREGATOR_1 && content.getCANSSCONTENTDESCRIPTION_STATUS() <= basic.LOC_AGGREGATOR_2)
								numof_uploading++;
							else if(content.getCANSSCONTENTDESCRIPTION_STATUS() >= basic.LOC_ENCODER_1 && content.getCANSSCONTENTDESCRIPTION_STATUS() <= basic.LOC_ENCODER_2)
							{
								if(content.getCANSSCONTENTDESCRIPTION_STATUS()==basic.LOC_ENCODER_2)
									numof_encoded++;
								else
									numof_encoding++;
							}
						}
						writer.addElement("numof_contents_pub", ""+numof_pub);
						writer.addElement("numof_contents_uploading", ""+numof_uploading);
						writer.addElement("numof_contents_encoded", ""+numof_encoded);
						writer.addElement("numof_contents_encoding", ""+numof_encoding);
					}
				}
				/*
				 * feeds
				 */
				if(!basic.isEmpty(_pid))
				{
					Feed[] feeds = control.getFeedStatusByPID(_pid);
					if(feeds!=null)
					{
						writer.addElement("numof_feeds", ""+feeds.length);
						int count = 0;
						for(Feed feed : feeds)
							if(!basic.isEmpty(feed.getRULEID()))
								count++;
						writer.addElement("numof_feeds_ruled", ""+count);
					}
				}
			} 
			writer.closeElement();
			writer.finish();
		}
	
	}
}